//
//  XQAccureIdentityInfoPop.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/24.
//

#import "XQAccureIdentityInfoPop.h"
#import "XQIdentityInfoPopSubView.h"
#import "XQIdentityPopSubCell.h"

@interface XQAccureIdentityInfoPop()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIView *centerView;
@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UIButton *closeButton;
@property (nonatomic,strong) UIButton *confimButton;
@property (nonatomic,strong) XQCustomEngeLabel *tipLb;
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation XQAccureIdentityInfoPop

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.centerView];
    [self.centerView addSubview:self.imageView];
    [self.imageView addSubview:self.closeButton];
    [self addSubview:self.confimButton];
    [self.imageView addSubview:self.tipLb];
    [self.imageView addSubview:self.tableView];
}

- (void)makeConstraints {
    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
        (void)make.center;
    }];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(([UIScreen mainScreen].height - 502 - 55) / 2);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.height.mas_equalTo(502);
    }];
    [self.confimButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).offset(10);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.height.mas_equalTo(45);
        make.bottom.equalTo(@0);
    }];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@12);
        make.right.equalTo(@-10);
        make.height.width.mas_equalTo(22);
    }];
    [self.tipLb mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.centerX;
        make.top.equalTo(@60);
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
        make.height.mas_equalTo(68);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tipLb.mas_bottom).offset(10);
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
        make.height.mas_equalTo(0);
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataSource[indexPath.row];
    XQIdentityPopSubCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XQIdentityPopSubCell" forIndexPath:indexPath];
    [cell setTitle:dic[@"causing"]];
    [cell setContent:dic[@"resulting"]];
    [cell setShowArrow:[dic[@"concept"] isEqualToString:@"syndrome"]];
    @weakify(self);
    cell.textFielHandle = ^(NSString * _Nonnull text) {
        @strongify(self);
        [self textFieldClick:dic[@"concept"] text:text];
    };
    cell.buttonHandle = ^{
        @strongify(self);
        if (self.birthdayHandle) {
            self.birthdayHandle();
        }
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 88;
}

- (void)textFieldClick:(NSString *)key text:(NSString *)text {
    if ([key isEqualToString:@"chance"]) {
        if (self.textFieldNameHandle) {
            self.textFieldNameHandle(text);
        }
    }else if ([key isEqualToString:@"did"]) {
        if (self.textFieldNumberHandle) {
            self.textFieldNumberHandle(text);
        }
    }
}

#pragma mark - setter

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    [self.tableView reloadData];
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(dataSource.count * 88);
    }];
}

- (void)setBirthday:(NSString *)birthday {
    NSMutableArray *list = self.dataSource.mutableCopy;
    for (NSDictionary *dic in list) {
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:dic];
        if ([dic[@"concept"] isEqualToString:@"syndrome"]) {
            dictionary[@"resulting"] = birthday;
            NSInteger index = [list indexOfObject:dic];
            list[index] = dictionary;
        }
    }
    _dataSource = list;
    [self.tableView reloadData];
}

- (void)setTip:(NSString *)tip {
    self.tipLb.text = tip;
}

#pragma mark - private

- (void)confimClick {
    if (self.confimHandle) {
        self.confimHandle(NO);
    }
}

- (void)bgClick {
    if (self.bgHandle) {
        self.bgHandle();
    }
}

- (void)closeClick {
    if (self.confimHandle) {
        self.confimHandle(YES);
    }
}

#pragma mark - lazy

- (UIView *)centerView {
    if (!_centerView) {
        _centerView = [[UIView alloc] init];
    }
    return _centerView;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.image = [UIImage imageNamed:@"identity_info_pop"];
        [_imageView setUserInteractionEnabled:YES];
        [_imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgClick)]];
    }
    return _imageView;
}

- (UIButton *)confimButton {
    if (!_confimButton) {
        _confimButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confimButton setTitle:@"Confirm" forState:UIControlStateNormal];
        [_confimButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confimButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        _confimButton.backgroundColor = [UIColor blueColor];
        _confimButton.layer.cornerRadius = 22.5;
        [_confimButton addTarget:self action:@selector(confimClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confimButton;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setImage:[UIImage imageNamed:@"pop_close"] forState:UIControlStateNormal];
        [_closeButton addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

- (XQCustomEngeLabel *)tipLb {
    if (!_tipLb) {
        _tipLb = [[XQCustomEngeLabel alloc] init];
        _tipLb.font = [UIFont systemFontOfSize:14];
        _tipLb.textColor = [UIColor whiteColor];
        _tipLb.textAlignment = NSTextAlignmentCenter;
        _tipLb.backgroundColor = kOrangeColor;
        _tipLb.numberOfLines = 0;
        _tipLb.layer.cornerRadius = 15;
        _tipLb.layer.masksToBounds = YES;
        [_tipLb setEdgtInsets:UIEdgeInsetsMake(0, 12, 0, 12)];
    }
    return _tipLb;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tableView.scrollEnabled = NO;
        [_tableView registerClass:[XQIdentityPopSubCell class] forCellReuseIdentifier:@"XQIdentityPopSubCell"];
    }
    return _tableView;
}

@end
